package org.trackcore.handler;

import org.trackcore.handler.DataHandler;
import org.trackcore.model.entity.BehaviorPv;
import org.trackcore.service.BehaviorPvService;
import org.trackcore.utils.DataUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 处理页面访问(PV)行为数据
 * 处理器Key: "behavior_pv"
 */
@Component
public class BehaviorPvHandler implements DataHandler {

    private final BehaviorPvService pvService;

    public BehaviorPvHandler(BehaviorPvService pvService) {
        this.pvService = pvService;
    }

    @Override
    public String getHandlerKey() {
        return "behavior_pv"; // 对应type=behavior, subType=pv
    }

    @Override
    public void handle(Map<String, Object> data, String logId) {
        BehaviorPv pv = BehaviorPv.builder()
                .logId(logId)
                .startTime(DataUtils.getDouble(data, "StarTime"))
                .pageUrl(DataUtils.getString(data, "pageUrl"))
                .referrer(DataUtils.getString(data, "referrer"))
                .uuid(DataUtils.getString(data, "uuid"))
                .build();

        pvService.save(pv);
    }
}
